package com.mlx.mbg.code.method;

import com.mlx.mbg.code.inter.EnableMethod;
import com.mlx.mbg.code.config.MbgPropertyConfig;
import com.mlx.mbg.code.inter.ExtensionLevel;
import com.mlx.mbg.code.inter.IExtensionMethod;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.Collections;
import java.util.List;

@EnableMethod
public class InsertMethod implements IExtensionMethod {

    @Override
    public boolean checkConfig(ExtensionLevel level, MbgPropertyConfig mbgPropertyConfig, IntrospectedTable introspectedTable) {
        return getExtensionLevelSet().contains(level);
    }

    @Override
    public int getOrder() {
        return 100;
    }

    @Override
    public List<Method> buildMethod(IntrospectedTable introspectedTable) {
        JavaVisibility methodVisibility = JavaVisibility.PUBLIC;

        FullyQualifiedJavaType methodReturnType = new FullyQualifiedJavaType("int");

        String methodName = "insert";

        Parameter methodParameter = new Parameter(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()), "data");

        Method method = new Method();
        method.setVisibility(methodVisibility);
        method.setReturnType(methodReturnType);
        method.setName(methodName);
        method.addParameter(methodParameter);
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * 单条插入");
        method.addJavaDocLine(" */");

        return Collections.singletonList(method);
    }

    @Override
    public List<XmlElement> buildSQL(IntrospectedTable introspectedTable) {
        XmlElement insert = new XmlElement("insert");
        insert.addAttribute(new Attribute("id", "insert"));

        TextElement text1 = new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime());

        XmlElement trimColumn = new XmlElement("trim");
        trimColumn.addAttribute(new Attribute("prefix", "("));
        trimColumn.addAttribute(new Attribute("suffix", ")"));
        trimColumn.addAttribute(new Attribute("suffixOverrides", ","));

        XmlElement includeColumn = new XmlElement("include");
        includeColumn.addAttribute(new Attribute("refid", "Base_Column_List"));
        trimColumn.addElement(includeColumn);

        TextElement text2 = new TextElement("values");

        XmlElement trimData = new XmlElement("trim");
        trimData.addAttribute(new Attribute("prefix", "("));
        trimData.addAttribute(new Attribute("suffix", ")"));
        trimData.addAttribute(new Attribute("suffixOverrides", ","));

        XmlElement includeProperty = new XmlElement("include");
        includeProperty.addAttribute(new Attribute("refid", "Insert_Property_List"));
        trimData.addElement(includeProperty);

        insert.addElement(text1);
        insert.addElement(trimColumn);
        insert.addElement(text2);
        insert.addElement(trimData);

        return Collections.singletonList(insert);
    }
}
